package com.chen.servicedriveruser.controller;

import com.chen.internalcommon.constant.DriverCarConstants;
import com.chen.internalcommon.dto.DriverUser;
import com.chen.internalcommon.dto.ResponseResult;
import com.chen.internalcommon.response.DriverUserExistsResponse;
import com.chen.internalcommon.response.OrderDriverResponse;
import com.chen.servicedriveruser.service.DriverUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenLiuPing
 **/
@RestController
@Slf4j
public class UserController {

    @Autowired
    private DriverUserService driverUserService;

    /**
     * 插入司机信息
     * @param driverUser 司机实体
     * @return
     */
    @PostMapping("/user")
    public ResponseResult addDriverUser(@RequestBody DriverUser driverUser) {
        log.info("司机信息:{}", driverUser);
        // 成功添加一个信息之后，后端要不要返回一个id呢
        return driverUserService.addDriverUser(driverUser);
    }

    /**
     * 修改司机信息
     * @param driverUser 司机实体
     * @return
     */
    @PutMapping("/user")
    public ResponseResult updateDriverUser(@RequestBody DriverUser driverUser) {
        log.info("修改的司机信息：{}", driverUser);
        return driverUserService.updateDriverUser(driverUser);
    }

    /**
     * 根据手机号查询对应司机是否存在
     * @param driverPhone
     * @return
     */
    @GetMapping("/check-driver/{driverPhone}")
    public ResponseResult<DriverUserExistsResponse> getUser(@PathVariable("driverPhone") String driverPhone) {
        ResponseResult<DriverUser> driverUserResponseResult = driverUserService.getDriverUserByPhone(driverPhone);
        DriverUser driverUser = driverUserResponseResult.getData();
        DriverUserExistsResponse driverUserExistsResponse = new DriverUserExistsResponse();
        driverUserExistsResponse.setDriverPhone(driverPhone);
        if (driverUser == null) {
            driverUserExistsResponse.setIfExists(DriverCarConstants.DRIVER_NOT_EXISTS);
        } else {
            driverUserExistsResponse.setIfExists(DriverCarConstants.DRIVER_EXISTS);
        }
        return ResponseResult.success(driverUserExistsResponse);
    }

    /**
     * 根据车辆Id查询订单需要的司机信息
     * @param carId 车辆id
     * @return
     */
    @GetMapping("/get-available-driver/{carId}")
    public ResponseResult<OrderDriverResponse> getAvailableDriver(@PathVariable("carId") Long carId) {
        return driverUserService.getAvailableDriver(carId);
    }
}
